﻿using BLL.Services;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace FlowerShopMVC.Controllers
{
    public class LoginController : Controller
    {
        private readonly UserService _userService = new UserService();
        // GET: Login
        // GET: /Login
        public ActionResult Index()
        {
            return View();
        }

        // POST: /Login
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Index(LoginViewModel model)
        {
            if (ModelState.IsValid)
            {
                object user = null;

                switch (model.UserType)
                {
                    case UserType.Tpeople:
                        user = _userService.ValidatePeopleCredentials(model.Username, model.Password);
                        break;
                    case UserType.Twoker:
                        user = _userService.ValidateWorkerCredentials(model.Username, model.Password);
                        break;
                    case UserType.Tuser:
                        user = _userService.ValidateUserCredentials(model.Username, model.Password);
                        break;
                }

                if (user != null)
                {
                    // 登录成功，可以重定向到主页或其他页面
                    return RedirectToAction("Index", "Home");
                }
                else
                {
                    ModelState.AddModelError("", "用户名或密码错误");
                }
            }

            // 如果验证失败，返回登录页面
            return View(model);
        }
    }
}
